package com.sgb.jicai.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.jicai.R;

/**
 * Time:2021/7/22
 * AUTHOR:F_Q
 * Description://集采自定义展开和收起控件
 */
public class SelectInputView extends RelativeLayout {
    private String tvName = "";
    private String etHint = "";
    private TextView selectInputTitle;
    private RelativeLayout open_close;
    private RelativeLayout rl_edit;
    private EditText et_select_input;
    private TextView edit_length;
    private View line_view;
    private ImageView iv_image;
    private boolean IsOpen ;

    public SelectInputView(Context context) {
        this(context, null);
    }

    public SelectInputView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public SelectInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.select_input_view, this);
        selectInputTitle = findViewById(R.id.select_input_title);
        open_close = findViewById(R.id.open_close);
        et_select_input = findViewById(R.id.et_select_input);
        edit_length = findViewById(R.id.edit_length);
        line_view = findViewById(R.id.line_view);
        rl_edit = findViewById(R.id.rl_edit);
        iv_image = findViewById(R.id.iv_image);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelectInputView);
            tvName = typedArray.getString(R.styleable.SelectInputView_tv_name);
            etHint = typedArray.getString(R.styleable.SelectInputView_et_hint);
            //设置输入框输入类型
            if (typedArray.getBoolean(R.styleable.SelectInputView_line_view, true)) {
                line_view.setVisibility(View.VISIBLE);
            } else {
                line_view.setVisibility(View.GONE);
            }
            TreatmentView(tvName, etHint);
            typedArray.recycle();
        }
        open_close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                IsOpen = !IsOpen;
                OpenCloseView(IsOpen);
            }
        });
        et_select_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                edit_length.setText(s.toString().length()+"");
            }
        });
    }

    //设置数据源数据
    private void TreatmentView(String tvName, String etHint) {
        if (!TextUtils.isEmpty(tvName)) {
            selectInputTitle.setText(tvName);
        }
        if (!TextUtils.isEmpty(etHint)) {
            et_select_input.setHint(etHint);
        }
    }

    //根据isOpen来判断用户是展开、还是收起
    private void OpenCloseView(boolean isOpen) {
        if (isOpen){//展开
            rl_edit.setVisibility(VISIBLE);
            line_view.setVisibility(View.GONE);
            iv_image.setImageResource(R.mipmap.btn_expand_up_gray);
        }else {//收起
            rl_edit.setVisibility(GONE);
            line_view.setVisibility(View.VISIBLE);
            iv_image.setImageResource(R.mipmap.btn_expand1);
        }
    }

    //获取当前输入的数据值
    public String getEditText(){
        String etName = et_select_input.getText().toString().trim();
        if (!TextUtils.isEmpty(etName)){
            return etName;
        }else {
            return "";
        }
    }
    public EditText getContent_edt() {
        return et_select_input;
    }
}
